package com.example.plist.util;


import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicSessionCredentials;
import com.qcloud.cos.region.Region;
import com.qcloud.cos.transfer.TransferManager;
import com.qcloud.cos.transfer.TransferManagerConfiguration;
import com.tencent.cloud.CosStsClient;
import com.tencent.cloud.Response;
import lombok.Data;


import java.io.IOException;
import java.util.TreeMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Data
public class CosFile {
    private String region="ap-shanghai";
    private String bucket="static-1307389591";
    private String SecretId="AKID98qHRLzLRL1QXNxGI2WjfKhoeEXaMq3z";
    private String SecretKey="g5D4f04rOffPs4zcy4tWt0qOfgRv84k8";
    private String tmpSecretId="";
    private String tmpSecretKey="";
    private String sessionToken="";
    // 创建 TransferManager 实例，这个实例用来后续调用高级接口
    public TransferManager createTransferManager() throws IOException {
        // 创建一个 COSClient 实例，这是访问 COS 服务的基础实例。
        // 详细代码参见本页: 简单操作 -> 创建 COSClient
        COSClient cosClient = this.GetCosClient();

        // 自定义线程池大小，建议在客户端与 COS 网络充足（例如使用腾讯云的 CVM，同地域上传 COS）的情况下，设置成16或32即可，可较充分的利用网络资源
        // 对于使用公网传输且网络带宽质量不高的情况，建议减小该值，避免因网速过慢，造成请求超时。
        ExecutorService threadPool = Executors.newFixedThreadPool(32);

        // 传入一个 threadpool, 若不传入线程池，默认 TransferManager 中会生成一个单线程的线程池。
        TransferManager transferManager = new TransferManager(cosClient, threadPool);

        // 设置高级接口的配置项
        // 分块上传阈值和分块大小分别为 5MB 和 1MB
        TransferManagerConfiguration transferManagerConfiguration = new TransferManagerConfiguration();
        transferManagerConfiguration.setMultipartUploadThreshold(5*1024*1024);
        transferManagerConfiguration.setMinimumUploadPartSize(1*1024*1024);
        transferManager.setConfiguration(transferManagerConfiguration);

        return transferManager;
    }
    public void shutdownTransferManager(TransferManager transferManager){
        transferManager.shutdownNow(true);
    }
   public COSClient GetCosClient() throws IOException {
       this.GetCredential();
       BasicSessionCredentials cred = new BasicSessionCredentials(tmpSecretId, tmpSecretKey, sessionToken);
       Region region = new Region(this.region);
       ClientConfig clientConfig = new ClientConfig(region);
       return new COSClient(cred, clientConfig);
   }
   public String getFullPath(String key){
       return  String.format("https://%s.cos.%s.myqcloud.com/%s",this.bucket,this.region,key);
    }

   public CosFile GetCredential() throws IOException {
        TreeMap<String, Object> config = new TreeMap<String, Object>();
        config.put("secretId", this.SecretId);
        // 云 api 密钥 SecretKey
        config.put("secretKey", this.SecretKey);
        config.put("durationSeconds", 1800);
        config.put("bucket", this.bucket);
        // 换成 bucket 所在地区
        config.put("region", this.region);

        String[] allowActions = new String[] {
                // 简单上传
                "name/cos:PutObject",
                "name/cos:PostObject",
                "name/cos:GetObject",
                // 分片上传
                "name/cos:InitiateMultipartUpload",
                "name/cos:ListMultipartUploads",
                "name/cos:ListParts",
                "name/cos:UploadPart",
                "name/cos:CompleteMultipartUpload"
        };
        config.put("allowActions", allowActions);
       config.put("allowPrefixes", new String[] {
               "*"
       });



//
        Response response = CosStsClient.getCredential(config);
        this.tmpSecretId = response.credentials.tmpSecretId;
        this.tmpSecretKey=response.credentials.tmpSecretKey;
        this.sessionToken=response.credentials.sessionToken;


        return this;
    }
}
